package vn.edu.saigontech.ncms.web.bookstore.report;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import vn.edu.saigontech.ncms.model.bookstore.ImportReceipt;
import vn.edu.saigontech.ncms.model.bookstore.ImportReceiptDetail;
import vn.edu.saigontech.ncms.model.bookstore.Item;
import vn.edu.saigontech.ncms.model.bookstore.Order;
import vn.edu.saigontech.ncms.model.bookstore.OrderDetail;
import vn.edu.saigontech.ncms.model.system.Parameter;
import vn.edu.saigontech.ncms.service.ImportReceiptService;
import vn.edu.saigontech.ncms.service.OrderService;
import vn.edu.saigontech.ncms.service.ParameterService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.util.I18n;

public class DetailReport {
	private String fromDate;
	private String toDate;
	private List<DetailImport> bookImportDetails;
	private List<DetailImport> cdImportDetails;
	private List<DetailExport> bookExportDetails;
	private List<DetailExport> cdExportDetails;
	private String lep;
	private String bsr;
	
	public DetailReport(Date fromDate, Date toDate) {
		this.fromDate = DateUtil.toString(fromDate, new I18n().getMessage("format.vndate"));
		this.toDate = DateUtil.toString(toDate, new I18n().getMessage("format.vndate"));
		this.bookImportDetails = new ArrayList<DetailImport>();
		this.cdImportDetails = new ArrayList<DetailImport>();
		this.bookExportDetails = new ArrayList<DetailExport>();
		this.cdExportDetails = new ArrayList<DetailExport>();
		this.init(fromDate, toDate);
	}

	private void init(Date fromDate, Date toDate) {
		List<ImportReceipt> importReceipts = ImportReceiptService.INSTANCE.findByDate(fromDate, toDate);
		for(ImportReceipt each : importReceipts) {
			for(ImportReceiptDetail detail : each.getDetails()) {
				Item item = detail.getItem();
				if(item.isBook()) {
					this.bookImportDetails.add(new DetailImport(each, detail));
				} else if(item.isCD()) {
					this.cdImportDetails.add(new DetailImport(each, detail));
				}
			}
		}
		List<Order> orders = OrderService.INSTANCE.findByDateAsc(fromDate, toDate);
		for(Order each : orders) {
			for(OrderDetail detail : each.getDetails()) {
				Item item = detail.getItem();
				if(item.isBook()) {
					this.bookExportDetails.add(new DetailExport(each, detail));
				} else if(item.isCD()) {
					this.cdExportDetails.add(new DetailExport(each, detail));
				}
			}
		}
		this.lep = ParameterService.INSTANCE.findText(Parameter.LEP);
		this.bsr = ParameterService.INSTANCE.findText(Parameter.BSR);
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public List<DetailImport> getBookImportDetails() {
		return bookImportDetails;
	}

	public void setBookImportDetails(List<DetailImport> bookImportDetails) {
		this.bookImportDetails = bookImportDetails;
	}

	public List<DetailImport> getCdImportDetails() {
		return cdImportDetails;
	}

	public void setCdImportDetails(List<DetailImport> cdImportDetails) {
		this.cdImportDetails = cdImportDetails;
	}

	public List<DetailExport> getBookExportDetails() {
		return bookExportDetails;
	}

	public void setBookExportDetails(List<DetailExport> bookExportDetails) {
		this.bookExportDetails = bookExportDetails;
	}

	public List<DetailExport> getCdExportDetails() {
		return cdExportDetails;
	}

	public void setCdExportDetails(List<DetailExport> cdExportDetails) {
		this.cdExportDetails = cdExportDetails;
	}

	public String getLep() {
		return lep;
	}

	public void setLep(String lep) {
		this.lep = lep;
	}

	public String getBsr() {
		return bsr;
	}

	public void setBsr(String bsr) {
		this.bsr = bsr;
	}
}